package net.contrend.material.dao;

import java.sql.Types;
import java.util.List;
import java.util.UUID;

import net.contrend.material.form.MaterialStorehouseForm;
import net.contrend.material.mapper.MaterialStorehouseMapper;
import net.contrend.system.dao.BaseDao;
import net.contrend.util.SqlUtils;
import net.contrend.util.StringUtils;

import org.springframework.jdbc.core.JdbcTemplate;

/**
 * <p>Copyright @ 2009 www.contrend.net</p>
 * <p>All right reserved. </p>
 * <p>项目名称			： 进销存管理系统 </p>
 * <p>JDK使用版本号		： jdk6.0 </P>
 * <p>描述				： 原料库 Dao </p>
 * @版本					： 1.0.0 
 * @author				： 谢庚才
 *
 * <p>修改历史 </p>
 * <p>修改时间            修改人员    修改内容 </p>
 * <p>Dec 11, 2009       谢庚才     新建 </p>
 */
public class MaterialStorehouseDao extends BaseDao {
	private String table = "material_storehouse";

	public MaterialStorehouseDao() {
	}
	
	public MaterialStorehouseDao(JdbcTemplate jt) {
        this.jt = jt;
    }
	
	public int insert(MaterialStorehouseForm materialStorehouse) {
		String columns = "msId, code, name, location, telephone, chargeMan, area, createDate, status";
        String sql = SqlUtils.getInsertSql(this.table, columns);
        System.out.println(String.format("\n%1$s\n", sql));
        
        if (StringUtils.isBlank(materialStorehouse.getMsId())) {
        	materialStorehouse.setMsId(UUID.randomUUID().toString());
        }
        
        return this.jt.update(sql,
                new Object[]{
        			materialStorehouse.getMsId(),
        			materialStorehouse.getCode(),
        			materialStorehouse.getName(),
        			materialStorehouse.getLocation(),
        			materialStorehouse.getTelephone(),
        			materialStorehouse.getChargeMan(),
        			materialStorehouse.getArea(),
        			materialStorehouse.getCreateDate(),
        			materialStorehouse.getStatus()
                },
                new int[]{
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.DOUBLE,
                    Types.DATE,
                    Types.VARCHAR
                });
	}
	
	public int update(MaterialStorehouseForm materialStorehouse) {
		String columns = "code, name, location, telephone, chargeMan, area, createDate, status";
        String sql = SqlUtils.getUpdateSql(this.table, columns, "msId = ?");
        System.out.println(String.format("\n%1$s\n", sql));
        return this.jt.update(sql,
        		new Object[]{
    			materialStorehouse.getCode(),
    			materialStorehouse.getName(),
    			materialStorehouse.getLocation(),
    			materialStorehouse.getTelephone(),
    			materialStorehouse.getChargeMan(),
    			materialStorehouse.getArea(),
    			materialStorehouse.getCreateDate(),
    			materialStorehouse.getStatus()
            },
            new int[]{
                Types.VARCHAR,
                Types.VARCHAR,
                Types.VARCHAR,
                Types.VARCHAR,
                Types.VARCHAR,
                Types.DOUBLE,
                Types.DATE,
                Types.VARCHAR
            });
	}
	
	public int delete(String msId) {
        String sql = SqlUtils.getDeleteSql(this.table, "msId=?");
        System.out.println(String.format("\n%1$s\n", sql));
        return this.jt.update(sql, new Object[]{msId}, new int[]{Types.VARCHAR});
    }
	
	@SuppressWarnings("unchecked")
	public MaterialStorehouseForm queryById(String id) {
        String sql = "select * from "+this.table+" where msId = ?";
        List<MaterialStorehouseForm> materialStorehouseList = this.jt.query(sql,
                new Object[]{id}, new int[]{Types.VARCHAR}, new MaterialStorehouseMapper());
        if (null != materialStorehouseList && 0 < materialStorehouseList.size()) {
            return materialStorehouseList.get(0);
        }
        return null;
    }
}
